from PIL import Image
import os
import glob
import subprocess

def delWepb(fileList):
    for filename in fileList:
        os.remove(filename)
def getJpgList(path):
    pathname =  os.path.join(path, "*.jpg")
    return glob.glob(pathname)

def getWebpList(path):
    pathname =  os.path.join(path, "*.webp")
    return glob.glob(pathname)

def webp_to_jpg(input_name_list):
    res=[]
    cmd = "dwebp.exe"
    for filename in input_name_list:
        newFileName = filename + ".jpg"
        subprocess.run([cmd,filename,'-o', newFileName])
        res.append(newFileName)
    return res


"""本硕博，40"""
def imgCutBottom(input_name_list, cutSize=40):
    for filename in input_name_list:
        # filename = '../640.jpg'
        pil_im = Image.open(filename)

        # print(pil_im.size)
        newSize = (0, 0, pil_im.size[0], pil_im.size[1] - cutSize)
        out = pil_im.crop(newSize)

        # import matplotlib.pyplot as plt
        # plt.imshow(out)
        # plt.show()#需要调用show()方法，不然图像只会在内存中而不显示出来
        out.save(filename)

if __name__=='__main__':
    import sys

    if len(sys.argv) > 1:
        jpgFileList = getJpgList(sys.argv[1])

        # 第一步，获取webplist
        srcFileList = getWebpList(sys.argv[1])

        # 第二步，格式转换
        newFileList = webp_to_jpg(srcFileList)

        # 第三步，裁剪
        imgCutBottom(newFileList)
        imgCutBottom(jpgFileList)
        # 第四步，删除webp
        delWepb(srcFileList)
    else:
        print("place input dest dir")